package com.shsxt.crm.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shsxt.crm.dao.YgCustomerDao;
import com.shsxt.crm.dao.YgCustomerLossDao;
import com.shsxt.crm.dao.YgCustomerOrderDao;
import com.shsxt.crm.model.YgCustomer;
import com.shsxt.crm.model.YgCustomerLoss;
import com.shsxt.crm.model.YgCustomerOrder;
import com.shsxt.framework.constant.CrmConstant;

@Service
public class SystemJudgeCustomerLossService {

	@Autowired
	private YgCustomerDao ygCustomerDao;

	@Autowired
	private YgCustomerOrderDao ygCustomerOrderDao;

	@Autowired
	private YgCustomerLossDao ygCustomerLossDao;

	@Transactional
	public void systemJudgeCustomerLoss() {
		// 获取6个月 前注册账户
		List<YgCustomer> beforeSixMonthCustomer = ygCustomerDao.queryCustomerBeforeSixMonth();
		if (beforeSixMonthCustomer != null && beforeSixMonthCustomer.size() > 0) {
			Iterator<YgCustomer> itor = beforeSixMonthCustomer.iterator();
			while (itor.hasNext()) {
				// 获取到 客户
				YgCustomer customer = itor.next();
				// 查询 客户最近6个月的订单
				List<YgCustomerOrder> month = ygCustomerOrderDao.queryCusomterOderRecentSixMonth(customer.getId());
				boolean flag = isConformCustomerLoss(month);
				if (flag) {
					YgCustomerLoss loss = new YgCustomerLoss();
					loss.setCusNo(customer.getKhno());
					loss.setCusName(customer.getName());
					loss.setCusManager(customer.getCusManager());
					loss.setState(1);
					loss.setIsValid(1);
					loss.setConfirmLossTime(new Date());
					loss.setLossReason(CrmConstant.SYSTEM_JUDGE_CUSTOMERLOSS_REASON);
					loss.setCreateDate(new Date());
					loss.setUpdateDate(new Date());
					ygCustomerLossDao.saveSte(loss);
				}
			}
		}
	}

	/**
	 * 判断是否符合 流失客户
	 * 
	 * @param list
	 * @return
	 */
	public boolean isConformCustomerLoss(List<YgCustomerOrder> list) {
		if (list == null || list.size() <= 0) {
			return true;
		}
		return false;
	}
}
